package com.wechat.android.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.wechat.android.app.R;
import com.wechat.android.app.entity.MessageContactsModel;
import com.wechat.android.app.utils.glide.GlideUtils;
import com.wechat.android.app.widget.RoundImageView;

import java.util.LinkedList;
import java.util.List;


/**
 * Created by yang on 2018/1/12 0002.
 */
public class MessageContactsAdapter extends BaseRecyclerAdapter<MessageContactsModel, MessageContactsAdapter.MessageContactsHolder> implements RecyclerHeadersAdapter<RecyclerView.ViewHolder> {
    private Context context;
    private OnDelFriendsListener onDelFriend;

    public MessageContactsAdapter(Context context, List<MessageContactsModel> data, boolean slideEnabled, boolean reboundEnabled) {
        super(context, data, slideEnabled, reboundEnabled);
        this.context = context;
    }

    @Override
    public MessageContactsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_message_contacts_recycler, parent, false);
        return new MessageContactsHolder(view);
    }

    @Override
    public void onBindViewHolder(MessageContactsHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
    }


    @Override
    public long getHeaderId(int position) {
        return data.get(position).getSortLetters().charAt(0);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.recycler_contacts_header, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView textView = (TextView) holder.itemView.findViewById(R.id.tv_recycler_header);
        String showValue = String.valueOf(data.get(position).getSortLetters().charAt(0));
        textView.setText(showValue);
    }

    public int getPositionForSection(char section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = data.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;

    }

    public String[] getArraySortLetters() {
        if (data.size() == 0) {
            return null;
        } else {
            LinkedList<String> listSortLetters = new LinkedList<>();
            for (int i = 0; i < data.size(); i++) {
                if (i == 0 || !listSortLetters.getLast().equals(data.get(i).getSortLetters().toUpperCase())) {
                    listSortLetters.add(data.get(i).getSortLetters().toUpperCase());
                }
            }
            String[] arraySortLetters = new String[listSortLetters.size()];
            arraySortLetters = listSortLetters.toArray(arraySortLetters);
            return arraySortLetters;
        }
    }

    public class MessageContactsHolder extends BaseRecyclerAdapter.ViewHolder {
        private RoundImageView ivAvatar;
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvDelete;


        public MessageContactsHolder(View view) {
            super(view);
            ivAvatar = (RoundImageView) view.findViewById(R.id.iv_message_contacts_item_avatar);
            tvTitle = (TextView) view.findViewById(R.id.tv_message_contacts_item_title);
            tvContent = (TextView) view.findViewById(R.id.tv_message_contacts_item_content);
            tvDelete = (TextView) view.findViewById(R.id.tv_delete);
        }

        public void bindData(final MessageContactsModel mMembers) {
            tvTitle.setText(mMembers.getName());
            tvContent.setText(mMembers.getPhone());
            tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onDelFriend != null) {
                        onDelFriend.onDelFriendOnClick(mMembers,getAdapterPosition());
                    }
                }
            });
            if (mMembers.getLogo() != null && !mMembers.getLogo().isEmpty()) {
                GlideUtils.loadImage(context, mMembers.getLogo(), ivAvatar, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                    @Override
                    public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                    }

                    @Override
                    public void onLoadingError(String source, Exception e) {

                    }
                }, R.mipmap.em_default_avatar, R.mipmap.em_default_avatar);


            } else {
                ivAvatar.setImageResource(R.mipmap.em_default_avatar);
            }

        }
    }

    public interface OnDelFriendsListener {
        void onDelFriendOnClick(MessageContactsModel data,int position);
    }

    public void setDelFriendOnClick(OnDelFriendsListener onDelFriend) {
        this.onDelFriend = onDelFriend;
    }
    public void remove(int position) {
        data.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, data.size() - position);
    }
}
